package com.mazaiting.redeye.api

import android.util.Base64
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.bean.ResponseDetectImageResultBean
import com.mazaiting.redeye.bean.RequestFaceImageBean
import com.mazaiting.redeye.bean.ResponseFaceTokenBean
import io.reactivex.Observable

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-08
 * @description 人脸 API
 */
class FaceApi constructor(private val service: FaceApiService){
    companion object {
        private var sApi: FaceApi? = null
        /**
         * 单例
         * @param service 服务
         */
        fun getInstance(service: FaceApiService): FaceApi {
            if (null == sApi) {
                synchronized(FaceApi::class.java) {
                    if (null == sApi) {
                        sApi = FaceApi(service)
                    }
                }
            }
            return sApi!!
        }
    }

    /**
     * 获取 Token
     */
    fun getAccessToken(): Observable<ResponseFaceTokenBean> {
        return service.getAccessToken(Config.FACE_GRANT_TYPE, Config.FACE_CLIENT_ID, Config.FACE_CLIENT_SECRET)
    }

    /**
     * 检测
     * @param data 图片数组
     */
    fun detect(accessToken: String, data: ByteArray): Observable<ResponseDetectImageResultBean> {
        // Base64 编码的图片字符串
        val image = Base64.encodeToString(data, Base64.DEFAULT)
        // 脸部类型字段
//        val faceField = "age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type"
//        val faceField = "age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type"
        val faceField = "quality"
        // 图片类型
        val imageType = "BASE64"
        // 脸部图片
        val faceImage = RequestFaceImageBean(image, faceField, imageType)
        return service.detect(accessToken, faceImage)
    }

}